//
//  ReplayToolsView.swift
//  SmartPenSwift
//
//  Created by tql on 2021/3/11.
//

import UIKit

class ReplayToolsView: UIView {

    private var inReplayLevel : Int = 1
    var replayLevel : Int {
        get{
            return inReplayLevel
        }
        set{
            inReplayLevel = newValue
            self.replaySpeedBtn.setTitle("\(newValue)X", for: .normal)
        }
    }
    private var  inProgress : CGFloat = 0
    var progress : CGFloat {
        get{
            return inProgress
        }set{
            inProgress = newValue
            self.progressView.progress = Float(inProgress)
            if inProgress >= 1.0 {
                self.pauseBtn.isSelected = true
                if self.PauseActionBlock != nil{
                    self.PauseActionBlock!(true)
                }
            }
        }
    }
 
    typealias CallBackBlock1 = (_ changeLevel : Int) -> ()
    typealias CallBackBlock2 = (_ pause : Bool) -> ()
    var PauseActionBlock : CallBackBlock2?
    var changeSpeedBlock : CallBackBlock1?
    
    lazy var progressView : UIProgressView = {
        let view : UIProgressView  = UIProgressView(progressViewStyle: .bar)
        view.progressTintColor = UIColor(hex: "EC6938")
        view.trackTintColor = UIColor(hex: "D6D8E4")
        return view
    }()
    
  
    
    
    lazy var replaySpeedBtn : UIButton = { [weak self] in
        let view : UIButton  = UIButton()
        view.addTarget(self, action: #selector(btnclick(_ :)), for: .touchUpInside)
        view.setTitleColor(UIColor(hex: "585E77"), for: .normal)
        view.setTitle("1X", for: .normal)
        view.layer.cornerRadius = 5
        view.layer.borderColor = UIColor(hex: "585E77").cgColor
        view.layer.borderWidth = 1.0
        view.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        return view
    }()
    @objc func btnclick(_ btn : UIButton ){
        debugPrint(#function)
        if inReplayLevel < 5 {
            self.replayLevel = inReplayLevel + 1
        }else{
            self.replayLevel = 1
        }
        
        if self.changeSpeedBlock != nil {
            self.changeSpeedBlock!(inReplayLevel)
        }
    }
    
    lazy var pauseBtn : UIButton = {
        let view : UIButton = UIButton()
        
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.random()
        self.initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func  initUI() -> Void {
        self.addSubview(self.pauseBtn)
        self.pauseBtn.snp.makeConstraints { (make) in
            make.width.height.equalTo(40)
            make.centerY.equalTo(self)
            make.left.equalTo(self).offset(20)
        }
        self.pauseBtn.addTarget(self, action: #selector(bntPauseclick(_ :)), for: .touchUpInside)
        self.pauseBtn.setImage(UIImage(named: "icon-暂停按钮"), for: .normal)
        self.pauseBtn.setImage(UIImage(named: "icon-播放按钮"), for: .selected)
        
        self.addSubview(self.progressView)
        self.progressView.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-80)
            make.left.equalTo(self).offset(80)
            make.height.equalTo(5)
            make.centerY.equalTo(self)
        }
        
        self.addSubview(self.replaySpeedBtn)
        self.replaySpeedBtn.snp.makeConstraints { (make) in
            make.width.height.equalTo(40)
            make.centerY.equalTo(self)
            make.right.equalTo(self).offset(-20)
        }
        
        
        
        
    }
    
    @objc func bntPauseclick(_ btn : UIButton){
        self.pauseBtn.isSelected = !self.pauseBtn.isSelected
        if self.progressView.progress >= 1.0 {
            if self.changeSpeedBlock != nil{
                self.changeSpeedBlock!(inReplayLevel)
            }
        }else{
            if self.PauseActionBlock != nil {
                self.PauseActionBlock!(btn.isSelected)
            }
        }
    }
    
}
